package cn.yq.share.service2.impl;


import cn.yq.share.entity.Teacher;
import cn.yq.share.mapper2.TeacherMapper;
import cn.yq.share.model.QueryTeacherList;
import cn.yq.share.service2.TeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 007
 * @version V1.0
 * @Description:
 * @date 2018/11/29 18:39
 */
@Service("teacherServiceImpl")
public class TeacherServiceImpl extends BaseService<Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Teacher> queryTeacherList(Page<QueryTeacherList> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return teacherMapper.selectAll();
    }

    @Override
    public Map<String, Object> saveOrUpdateTeacher(Teacher Teacher) {
        LinkedHashMap<String,Object> resultMap=new LinkedHashMap<String,Object>();

        if(Teacher!=null){
            if(StringUtil.isNotEmpty(Teacher.getId())){//编辑
                if(StringUtil.isNotEmpty(Teacher.getName())){
                    updateNotNull(Teacher);
//                    System.out.println(1/0);
                    resultMap.put("state","success");
                    resultMap.put("message","修改班级成功");
                    return resultMap;
                }else{
                    resultMap.put("state","fail");
                    resultMap.put("message","修改失败，缺少字段");
                    return resultMap;
                }
            }else{//新建
                if(StringUtil.isNotEmpty(Teacher.getName())){
                    Teacher.setId(UUID.randomUUID().toString().replaceAll("-",""));
                    saveNotNull(Teacher);
                    resultMap.put("state","success");
                    resultMap.put("message","新建班级成功");
                    return resultMap;
                }else{
                    resultMap.put("state","fail");
                    resultMap.put("message","新建失败，缺少字段");
                    return resultMap;
                }
            }
        }else{
            resultMap.put("state","fail");
            resultMap.put("message","失败");
            return resultMap;
        }

    }
}
